// pages/user/questionnaire.js
var app = getApp();
var request = app.request;
var util = require('../../../utils/util.js');
var common = require('../../../utils/common.js');

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		url: app.globalData.setting.url,
		questionlist: null,
		question_form: [],
		answer: [],
		"opinion": {},
		"title": "调查问卷",
		"description": "问卷描述问卷描述问卷描述问卷描述问卷描述问卷描述问卷描述问卷描述问卷描述问卷描述问卷描述",
		"region": ['北京市', '北京市', '东城区']
	},
	bindRegionChange: function(e) {
		this.setData({
			region: e.detail.value
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.requestQuestionDetail();

	},
	changeInput: function(e) {
		//console.log(e.detail);
		// var that=this;
		// var list=that.data.questionlist;
		// var id=e.currentTarget.dataset.id;
		//that.data.answer.push({id:id,answer_item:e.detail.value});
		//that.data.answer.push({id:id,items:e.detail.value});
		// that.data.answer.push({
		//   title:id,
		//   value:e.detail.value.join(',')
		// })
		// that.data.answer["id-" + id] = e.detail.value;
		// console.log(that.data.answer)
		//return
		var that = this;
		var list = that.data.questionlist;
		var id = e.currentTarget.dataset.id;
		var idx = e.currentTarget.dataset.idx;
		var type = e.currentTarget.dataset.type;
		var question_form = e.detail.value;

		for (let i = 0; i < list.length; i++) {
			if (id == list[i].id) {
				if (type == 3) {
					list[i].question_form = question_form;
				} else if (type == 2) {
					for (let j = 0; j < list[i].toption.length; j++) {
						//list[i].toption[j].attr_index=0;
						// console.log(list[i].toption[j],111)
						if (question_form == list[i].toption[j].attr_name) {
							list[i].toption[j].attr_index = "1";
						}
					}
				} else {
					list[i].toption[j].attr_index = "1";
				}
			}
		}

		// for (var index in list) {
		//   console.log(idx)
		//   if (index == (idx-1)) {

		//     list[index].question_form = question_form;
		//   }
		// }
		this.setData({
			questionlist: list
		})
		//  this.$forceUpdate()

	},
	checkboxChange: function(e) {
		var that = this;
		var list = that.data.questionlist;
		var idx = e.currentTarget.dataset.idx;
		var id = e.currentTarget.dataset.id;
		var type = e.currentTarget.dataset.type;
		var answer = {
			id: id,
			type: type,
			value: e.detail.value
		};
		var question_form = [];

		// for (var index in list) {
		//   if (index == (idx-1)) {
		//     list[index].answer = answer;
		//     for (var index in question_form){
		//       console.log(index,question_form,18)
		//       question_form[index].item=list[index].answer;
		//     }
		//   }
		// }

		for (var index in list) {
			if (index == (idx - 1)) {
				list[index] = answer;
			}
			//question_form.push(question_form[index]);
		}
		// list.join(',');
		// console.log(list,77)
		this.setData({
			question_form: list
		})

	},
	// radioChange:function(e){
	//   console.log(e,111);
	//   var that=this;
	//   var list=that.data.questionlist;
	//   var idx=e.currentTarget.dataset.idx;
	//   var answer=e.detail.value;
	//   for (var index in list) {
	//     if (index == (idx-1)) {
	//       list[index].answer = answer;
	//     }
	//   }
	//   this.setData({
	//     questionlist: list
	//   })
	//   console.log(that.data.questionlist)

	// },
	requestQuestionDetail: function() {
		var that = this;
		var url = '/api/question/index'
		// console.log(url);
		request.get(that.data.url + url, {
			success: function(res) {
				// console.log(res.data.result.question_list);
				var questionlists = res.data.result.question_list;
				that.setData({
					questionlist: questionlists,

				});
			}
		});
	},
	formSubmit: function(e) {
		var that = this;
		let data = [...that.data.question_form]
		let list = []
		data.forEach((e1, index) => {
			if (e1.type == 2) {
				let a = ''
				if (typeof e1.value == Array) {
					e1.value.forEach((s, index) => {
						a += index == 0 ? s : ',' + s
					})
					e1.value = a
				}
			}
			if (e1.type == 3 && e1.status == 1) {
				data[index] = {
					id: e1.id,
					type: e1.type,
					value: ''
				}
			}
		})
		wx.request({
			url: app.globalData.setting.url + "/api/question/add_answer" + '?token=' + app
				.globalData.userInfo.token,
			method: 'POST',
			data: {
				answer: data
			},
			header: {
				'Content-Type': 'application/json'
			},
			success: function(res) {
				if (res.data.status == 1) {
					wx.showModal({
						title: '提示',
						content: '调查问卷提交成功',
						showCancel: false
					})
					setTimeout(() => {
						wx.navigateBack();
					}, 2000)
				}
			}

		})
		// request.post('/api/question/add_answer', {
		//   header: {
		//     'content-type': 'application/json'
		//   },
		//   data: {answer:data},
		//   success: function (res) {
		//     console.log(res, 33);
		// 	if(res.data.status==1){
		// 		wx.showModal({
		// 		  title: '提示',
		// 		  content: '调查问卷提交成功',
		// 		  showCancel: false
		// 		})
		// 		setTimeout(()=>{
		// 		 wx.navigateBack();
		// 		},2000)
		// 	}
		//   }
		// });
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}

})